#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>


#include "common.h"


int login()
{

    return 0;
}

char* send_to_who(int* n_people)
{
    printf("how many people do you want to send?\n");

    scanf("%d\n",n_people);
    char* to_whos=(char*)malloc(sizeof(char)*(*n_people)*lenght_of_username);
    for(int i=0;i<*n_people;i++)
    {
        scanf("%s\n",to_whos+i*lenght_of_username);
        printf("%s\n",to_whos+i*lenght_of_username);
    }
    return to_whos;
}


int read_socket(int clientfd)
{
    char recv_buf[1024]={0};
    while(1)
    {
        ssize_t wd=read(clientfd,recv_buf,1024);
        if(wd>0)
        {
        printf("\nthe recv data is %s",recv_buf);
    }
    }
    
}
int main(int argc , char* argv[])
{
    char data_buf[800];
    char send_buf[1024];
    char recv_buf[1024];
    struct USER_PASSWD user1_info;
    bool login_status=false;
    
    // 创建套接字，返回文件符
    int clientfd=socket(AF_INET,SOCK_STREAM,0);
    // 定义套接字sockaddr_in 结构体
    struct sockaddr_in server_addr;
    // 结构体元素赋值
    server_addr.sin_family=AF_INET;
    
    server_addr.sin_addr.s_addr=inet_addr("172.17.0.1");
    // server_addr.sin_addr.s_addr=inet_addr("121.43.33.73");
    server_addr.sin_port=htons(11234);
    //连接套接字 
    connect(clientfd,(struct sockaddr *)&server_addr,sizeof(server_addr));
    
    do
    {    
        // 获取用户名和密码
        printf("username:");
        scanf("%s",user1_info.username);
        printf("password:");
        scanf("%s",user1_info.password);
        // printf("%s %s",user1_info.username,user1_info.password);
        
        // 向套接字文件符写入信息
        write(clientfd,&user1_info,sizeof(struct USER_PASSWD)+1);
        // 读取返回值
        ssize_t rd=read(clientfd,recv_buf,1024);
        char ret_status[5]={0};
        memcpy(ret_status,recv_buf,4);
        // printf("recv_msg is %s\n",recv_buf);
        login_status=strcmp(ret_status, "#@01");
        // printf("%s %s ",ret_status,"#@01");
        printf("%d",login_status);
        if(login_status)
        {
            printf("username or password error!\nplease reinput\n");
        }
    }
    while(login_status); 
    printf(" logined!\n");
    
    struct Message_Header message_header;
    
    memcpy(&message_header.username,&user1_info.username,lenght_of_username);
    char* to_whos=send_to_who(&message_header.n_people);

    printf("%s %d ",message_header.username,message_header.n_people);
    printf("%d ",sizeof(message_header));

    printf("%s\n",to_whos);
    for(int i=0;i<sizeof(to_whos)/lenght_of_username;i++)
    {
        printf("%s\n",to_whos+i*lenght_of_username);
    }
    
    
    // 创建线程   
    pthread_t tid;
    pthread_create(&tid,NULL,read_socket,clientfd);

    while (1)
    {
        scanf("%s",data_buf);
        printf("\ndata is :%s\n",data_buf);
        memset(send_buf,0,1024);
        memcpy(send_buf,&message_header,sizeof(struct Message_Header));
        memcpy(send_buf+sizeof(struct Message_Header),to_whos,message_header.n_people*lenght_of_username);
        memcpy(send_buf+sizeof(struct Message_Header)+message_header.n_people*lenght_of_username,data_buf,strlen(data_buf));
        
        // memset()
        // rw=write(clientfd,&message_header,sizeof(struct Message_Header));
        // rw=write(clientfd,message_header.n_people,sizeof(int));
        ssize_t rw=write(clientfd,send_buf,sizeof(send_buf));
        if(rw<0)
        {
            printf(" write error \n");
            break;
        }
        // ssize_t rd=read(clientfd,recv_buf,1024);
        // printf("%s\n",recv_buf);

    }

    // 关闭套接字
    close(clientfd);
    
    
    return 0;
    

}